package com.sfl.api_backend.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * @Author szc
 * @create 2023/2/2 21:34
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SecurityUser implements UserDetails {

    private Users users;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(users.getUserType() == 0){
            return List.of(new SimpleGrantedAuthority("Admin"), new SimpleGrantedAuthority("User"));
        }
        return List.of(new SimpleGrantedAuthority("User"));
    }

    @Override
    public String getPassword() {
        return users.getPassword();
    }

    @Override
    public String getUsername() {
        return users.getEmail();
    }

    @Override
    public boolean isAccountNonExpired() {
        return users.getStatus() == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return users.getStatus() == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return users.getStatus() == 1;
    }

    @Override
    public boolean isEnabled() {
        return users.getStatus() == 1;
    }
}
